package com.example.music_project.pages;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.music_project.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;
    private Fragment currentFragment; // 用于跟踪当前显示的 Fragment

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 启动
        // 初始化底部导航栏
        bottomNavigationView = findViewById(R.id.bottom_navigation);

        // 默认加载音乐 Fragment
        showMusicFragment();

        // 设置底部导航栏点击事件
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = item.getItemId();

                if (id == R.id.nav_music) {
                    showMusicFragment();
                    return true;
                } else if (id == R.id.nav_social) {
                    showSocialFragment();
                    return true;
                } else if (id == R.id.nav_my) {
                    showProfileFragment();
                    return true;
                }
                return false;
            }
        });
    }

    /**
     * 显示音乐列表 Fragment
     */
    private void showMusicFragment() {
        if (currentFragment instanceof MusicFragment) return; // 如果已经是 MusicFragment，则不重复加载

        Fragment musicFragment = new MusicFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, musicFragment);
        transaction.commit();
        currentFragment = musicFragment; // 更新当前 Fragment 引用
    }

    /**
     * 显示个人资料 Fragment
     */
    private void showProfileFragment() {
        if (currentFragment instanceof ProfileFragment) return; // 如果已经是 ProfileFragment，则不重复加载

        Fragment profileFragment = new ProfileFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, profileFragment);
        transaction.commit();
        currentFragment = profileFragment; // 更新当前 Fragment 引用
    }

    /**
     * 显示社交圈 Fragment
     */
    private void showSocialFragment() {
        if (currentFragment instanceof SocialFragment) return; // 如果已经是 SocialFragment，则不重复加载

        Fragment socialFragment = new SocialFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, socialFragment);
        transaction.commit();
        currentFragment = socialFragment; // 更新当前 Fragment 引用
    }

    /**
     * 处理返回键，避免直接退出应用
     */
    @Override
    public void onBackPressed() {
        if (bottomNavigationView.getSelectedItemId() != R.id.nav_music) {
            // 如果当前不在音乐页面，则返回音乐页面
            bottomNavigationView.setSelectedItemId(R.id.nav_music);
        } else {
            // 否则执行默认的返回操作
            super.onBackPressed();
        }
    }
}